/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2020年9月3日
 */
package com.massyframework.beanskin.runtime.modular.assembly.handling;

import java.util.concurrent.CopyOnWriteArraySet;

import com.massyframework.beanskin.assembly.handling.LifecycleListener;

/**
 * 生命周期事件发布者
 * 
 * <p>
 * 提供生命周期事件的监听和发布。
 * 
 * @author huangkh
 *
 */
abstract class LifecycleEventPublisher extends AbstractHandlerRegistry {

	private final CopyOnWriteArraySet<LifecycleListener> listeners;

	/**
	 * 构造方法
	 */
	public LifecycleEventPublisher() {
		this.listeners = new CopyOnWriteArraySet<LifecycleListener>();
	}
	
	/**
     * 添加事件监听器
     * @param listener {@link LifecycleListener}
     */
    public void addListener(LifecycleListener listener){
        if (listener != null){
            this.listeners.add(listener);
        }
    }
    
    /**
     * 发布进入准备就绪
     */
    protected synchronized void applyReadiedEvent() throws Exception{
        for (LifecycleListener listener: this.listeners){
            try{
                listener.onReadied();
            }catch(Exception e){
                this.logError("onReadied failed: listener=" + listener + ".", e);
                throw e;
            }
        }
    }
    
   
    /**
     * 发布装配件激活事件
     */
    protected void applyActivatedEvent() throws Exception{
        for (LifecycleListener listener: this.listeners){
            try{
                listener.onActivated();
            }catch(Exception e){
                this.logError("onActivated failed: listener=" + listener + ".", e);
                throw e;
            }
        }

    }

    /**
     * 发布装配件钝化事件
     */
    protected void applyInactivatingEvent() throws Exception{
        for (LifecycleListener listener: this.listeners){
            try{
                listener.onInactivating();
            }catch(Exception e){
                this.logError("onInactivating failed: listener=" + listener + ".", e);
                throw e;
            }
        }
    }

    /**
     * 发布装配件退出就绪事件
     */
    protected void applyUnreadyingEvent() throws Exception{
        for (LifecycleListener listener: this.listeners){
            try{
                listener.onUnreadying();
            }catch(Exception e){
                this.logWarn("onUnreadying failed: listener=" + listener + ".", e);
                throw e;
            }
        }
    }

    /**
     * 移除事件监听器
     * @param listener {@link LifecycleListener}
     */
    public void removeListener(LifecycleListener listener){
        if (listener != null){
            this.listeners.remove(listener);
        }
    }

}
